package com.ccy_blog.back.service.impl;

import com.ccy_blog.back.bean.Article;
import com.ccy_blog.back.bean.Notice;
import com.ccy_blog.back.mapper.NoticeMapper;
import com.ccy_blog.back.service.NoticeService;
import com.ccy_blog.base.exception.BlogEnum;
import com.ccy_blog.base.exception.BlogException;
import com.ccy_blog.base.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class NoticeServiceImpl implements NoticeService {
    
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void deleteById(String id) {
        int count = noticeMapper.deleteByPrimaryKey(id);
        if(count == 0){
            throw new BlogException(BlogEnum.NOTICE_DELETE);
        }
    }

    @Override
    public Notice saveOrUpdate(Notice notice) {
        if(notice.getNid() == null){
            //添加
            //发布时间
            notice.setCreate_time(DateTimeUtil.getSysTime());
            int count = noticeMapper.insertSelective(notice);
            if(count == 0){
                //发布失败
                throw new BlogException(BlogEnum.NOTICE_PUNISH);
            }
        }else{
            notice.setUpdate_time(DateTimeUtil.getSysTime());
            int count = noticeMapper.updateByPrimaryKeySelective(notice);
            if(count == 0){
                //修改失败
                throw new BlogException(BlogEnum.NOTICE_UPDATE);
            }
        }
        return notice;
    }

    @Override
    public Notice queryById(String id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        return notice;
    }

    @Override
    public List<Notice> list(String uid) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        //查询当前登录用户的文章
        criteria.andEqualTo("uid",uid);
        List<Notice> Notices = noticeMapper.selectByExample(example);
        return Notices;
    }
    @Override
    public List<Notice> list() {
        List<Notice> Notices = noticeMapper.selectAll();
        return Notices;
    }




}
